/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.File;
import java.io.FileInputStream;
import java.util.Locale;
import java.util.Properties;

import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource;

import br.ufmg.dees.insane.commons.view.SplashScreen;

/**
 * This is the main class of INSANE.
 * Sets Look and Feel, validates and centers interface.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @version 1.0
 * @since November 2003
 */
public class Insane {
    
	static SplashScreen splash;
	
//*****************************************************************************
	
    /**
     * Constructor method. <br>
     * Validates and centers interface
     */
    Insane() {
		
        InsaneInterface frame = new InsaneInterface(getLocale());
        frame.setLocale(getLocale());
        frame.validate();
        
        frame.addInternalFrame(new InsaneInternalInterface(frame.getResourceBundle()));
        
		//Centers the window
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension frameSize = frame.getSize();
        if (frameSize.height > screenSize.height)
            frameSize.height = screenSize.height;
        if (frameSize.width > screenSize.width)
            frameSize.width = screenSize.width;
        frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
      	frame.setVisible(true);
    }
    
//*****************************************************************************
	
    /**
     * Sets Look And Feel
     * Starts Aplication
     */
    public static void main(String[] args ) {
		try {
			Locale currentLocale = getLocale();
			
			splash = SplashScreen.getInstance();
			splash.setLocale(currentLocale);
			splash.openSplash();
			
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            Font fontCurrent = UIManager.getFont("Button.font");
            String name = fontCurrent.getName();
            
			Insane.advanceSplashProgress();
			
            //Creates similar font with the specified style and size
            FontUIResource fontResourceNew  = new FontUIResource(name, Font.PLAIN, 12);
            
            //Sets all items fonts
            UIManager.put("Button.font", fontResourceNew);
            UIManager.put("CheckBox.font", fontResourceNew);
            UIManager.put("CheckBoxMenuItem.font", fontResourceNew);
            UIManager.put("ColorChooser.font", fontResourceNew);
            UIManager.put("ComboBox.font", fontResourceNew);
            UIManager.put("DesktopIcon.font", fontResourceNew);
            UIManager.put("EditorPane.font", fontResourceNew);
            UIManager.put("InternalFrame.font", fontResourceNew);
            UIManager.put("Label.font", fontResourceNew);
            UIManager.put("List.font", fontResourceNew);
            UIManager.put("MenuBar.font", fontResourceNew);
            UIManager.put("Menu.font", fontResourceNew);
            UIManager.put("MenuItem.font", fontResourceNew);
            UIManager.put("OptionPane.font", fontResourceNew);
            UIManager.put("PasswordField.font", fontResourceNew);
            UIManager.put("PopupMenu.font", fontResourceNew);
            UIManager.put("ProgressBar.font", fontResourceNew);
            UIManager.put("RadioButton.font", fontResourceNew);
            UIManager.put("RadioButtonMenuItem.font", fontResourceNew);
            UIManager.put("ScrollPane.font", fontResourceNew);
            UIManager.put("TabbedPane.font", fontResourceNew);
            UIManager.put("TableHeader.font", fontResourceNew);
            UIManager.put("TextArea.font", fontResourceNew);
            UIManager.put("TextField.font", fontResourceNew);
            UIManager.put("TextPane.font", fontResourceNew);
            UIManager.put("TitledBorder.font", fontResourceNew);
            UIManager.put("ToggleButton.font", fontResourceNew);
            UIManager.put("ToolBar.font", fontResourceNew);
            UIManager.put("ToolTip.font", fontResourceNew);
            UIManager.put("Tree.font", fontResourceNew);
            UIManager.put("Viewport.font", fontResourceNew);
            
//          Internationalizing Swing Elements for portuguese
			if (currentLocale.getLanguage().equals(new Locale("pt", "", "").getLanguage())) {
				//ColorChooser
				UIManager.put("ColorChooser.sampleText","Texto Exemplo");
				UIManager.put("ColorChooser.swatchesNameText","Escolher");
				UIManager.put("ColorChooser.resetText", "Restaurar");
				UIManager.put("ColorChooser.previewText", "Pr\u00e9-Visualizar");
				UIManager.put("ColorChooser.cancelText", "Cancelar");
				UIManager.put("ColorChooser.swatchesRecentText", "Recentes:");
				UIManager.put("ColorChooser.rgbBlueText", "Azul");
				UIManager.put("ColorChooser.rgbGreenText", "Verde");
				UIManager.put("ColorChooser.rgbRedText", "Vermelho");
				//OptionPane
				UIManager.put("OptionPane.cancelButtonText", "Cancelar");
				UIManager.put("OptionPane.noButtonText", "N\u00e3o");
				UIManager.put("OptionPane.okButtonText", "OK");
				UIManager.put("OptionPane.yesButtonText", "Sim");
				//FileChooser
				UIManager.put("FileChooser.acceptAllFileFilterText", "Todos os Arquivos (*.*)");
				UIManager.put("FileChooser.cancelButtonText", "Cancelar");
				UIManager.put("FileChooser.cancelButtonToolTipText", "Abortar di\u00e1logo de escolha de arquivos");
				UIManager.put("FileChooser.detailsViewButtonAccessibleName", "Detalhes");
				UIManager.put("FileChooser.detailsViewButtonToolTipText", "Detalhes");
				UIManager.put("FileChooser.directoryDescriptionText", "Diret\u00f3rio");
				UIManager.put("FileChooser.fileDescriptionText", "Arquivo Gen\u00e9rico");
				UIManager.put("FileChooser.fileNameLabelText", "Nome do Arquivo:");
				UIManager.put("FileChooser.filesOfTypeLabelText", "Arquivos do tipo:");
				UIManager.put("FileChooser.helpButtonText", "Ajuda");
				UIManager.put("FileChooser.helpButtonToolTipText", "Ajuda do di\u00e1logo de escolha de arquivo");
				UIManager.put("FileChooser.homeFolderAccessibleName", "Home");
				UIManager.put("FileChooser.homeFolderToolTipText", "Home");
				UIManager.put("FileChooser.listViewButtonAccessibleName", "Listar");
				UIManager.put("FileChooser.listViewButtonToolTipText", "Listar");
				UIManager.put("FileChooser.lookInLabelText", "Procurar em:");
				UIManager.put("FileChooser.saveInLabelText", "Salvar em:");
				UIManager.put("FileChooser.newFolderAccessibleNam", "Nova Pasta");
				UIManager.put("FileChooser.newFolderErrorText", "Erro ao criar nova pasta:");
				UIManager.put("FileChooser.newFolderToolTipText", "Criar Nova Pasta");
				UIManager.put("FileChooser.openButtonText", "Abrir");
				UIManager.put("FileChooser.openButtonToolTipText", "Abrir arquivo selecionado");
				UIManager.put("FileChooser.saveButtonText", "Salvar");
				UIManager.put("FileChooser.saveButtonToolTipText", "Salvar arquivo selecionado");
				UIManager.put("FileChooser.updateButtonText", "Atualizar");
				UIManager.put("FileChooser.updateButtonToolTipText", "Atualizar lista de diret\u00f3rios");
				UIManager.put("FileChooser.upFolderAccessibleName", "Acima");
				UIManager.put("FileChooser.upFolderToolTipText", "Um n\u00edvel acima");
			}
        }
        catch (Exception e){}
        new Insane();
    }
	
//*****************************************************************************

	/** Reads the Insane.properties file and returns the specified Locale.
	* If there is an error reading the file, returns the system default Locale.
	*@return The Locale specified in the properties file.
	*/
	public static Locale getLocale() {
		Locale locale = Locale.getDefault();
		try {
			FileInputStream in = new FileInputStream(new File("lib/Insane.properties"));
			Properties p = new Properties();
			p.load(in);
			String lang = p.getProperty("language");
			if (lang.equals("default")) {
				locale = Locale.getDefault();
			}
			if (lang.equals("english")) {
				locale = new Locale("en", "", "");
			}
			if (lang.equals("portuguese")) {
				locale = new Locale("pt", "", "");
			}
		} catch (Exception e) {
			locale = Locale.getDefault();
		}
		return locale;
	}

//*****************************************************************************
	
	public static void advanceSplashProgress() {
		if(splash != null)
			splash.advance();
	}
	
//*****************************************************************************
}